#include "header.hpp"
using namespace std;

// Leetcode 48: Rotate Image
/* You are given an n x n 2D matrix representing an image,
 * rotate the image by 90 degrees (clockwise).
 * You have to rotate the image in-place,
 * which means you have to modify the input 2D matrix directly.
 * DO NOT allocate another 2D matrix and do the rotation.
*/
void rotate(vector<vector<int>>& matrix) {
    int n = matrix.size();
    for(int i = 0; i < n; i++){
        for(int j = i; j < n; j++){
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n / 2; j++){
            int temp = matrix[i][j];
            matrix[i][j] = matrix[i][n - j - 1];
            matrix[i][n - j - 1] = temp;
        }
    }
}

void test8(){
    //Test 1
    vector<vector<int>> matrix1{
        vector<int>{1, 2, 3},
        vector<int>{4, 5, 6},
        vector<int>{7, 8, 9},
    };

    rotate(matrix1);
    for_each(matrix1.begin(), matrix1.end(), [&](vector<int>& row){
        for(auto d : row){
            cout << d << " ";
        }
        cout << endl;
    });

    cout << endl;

    //Test 2
    vector<vector<int>> matrix2{
        vector<int>{5, 1, 9, 11},
        vector<int>{2, 4, 8, 10},
        vector<int>{13, 3, 6, 7},
        vector<int>{15, 14, 12, 16}
    };

    rotate(matrix2);
    for_each(matrix2.begin(), matrix2.end(), [&](vector<int>& row){
        for(auto d : row){
            cout << d << " ";
        }
        cout << endl;
    });
}
